package org.jiuling.xml.jaxb.adapter;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * LocalDateTime适配器
 * @author 紫灵
 * @date 2024年11月29日 20时19分28秒
 */
public class LocalDateTimeDateAdapter extends XmlAdapter<String, LocalDateTime> {
    @Override
    public LocalDateTime unmarshal(String s) {
        return LocalDateTime.ofInstant(
                Instant.ofEpochSecond(Long.parseLong(s)),
                ZoneId.systemDefault()
        );
    }

    @Override
    public String marshal(LocalDateTime localDateTime) {
        return String.valueOf(
                localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond()
        );
    }
}
